package com.fwzc.giuseppe.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.adapter.BannerAdapter;
import com.fwzc.giuseppe.adapter.BannerInfo;
import com.fwzc.giuseppe.adapter.HolderAdapter;
import com.fwzc.giuseppe.adapter.HolderAdapter.ViewCreator;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.config.Config;
import com.fwzc.giuseppe.view.BaseViewPager.OnPageChangeListener;
import com.fwzc.giuseppe.view.LoopPageIndicator;
import com.fwzc.giuseppe.view.LoopViewPager;
import com.fwzc.giuseppe.view.LoopViewPagerAdapter;
import com.lidroid.xutils.util.LogUtils;

public class CommodityDetails extends BaseActivity implements OnClickListener{
	private Context context = CommodityDetails.this;

	private LoopViewPager mViewPager;
	private LoopViewPagerAdapter mPagerAdapter;
	private LoopPageIndicator mPagerIndicator;
	private GridView gv;
	private PopupWindow pw;
	RelativeLayout select_size;
	HolderAdapter<HashMap<String,String>> mAdapter_grid ;
	private List<HashMap<String, String>> data_grid = new ArrayList<HashMap<String, String>>();
	Button immediately_buy;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.commodity_details);
		initBar();
		actionbar_side_name.setText("商品详情");
		bg_actionbar_side.setBackgroundColor(getResources().getColor(R.color.transparent));
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		iv_actionbar_side_left.setOnClickListener(this);
		initView();
	}

	private void initView() {
		mPagerIndicator = (LoopPageIndicator) findViewById(R.id.task_list_id_indicator);
		mViewPager = (LoopViewPager) findViewById(R.id.task_list_id_banner);
		select_size = (RelativeLayout) findViewById(R.id.select_size);
		select_size.setOnClickListener(this);
		immediately_buy = (Button) findViewById(R.id.immediately_buy);
		immediately_buy.setOnClickListener(this);
		gv = (GridView) findViewById(R.id.gridview);
		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				LogUtils.d("----------------------------");
				Intent intent = new Intent(context, ShoppingCart.class);
				startActivity(intent);
			}
		});
		createAdapterGrid();
		if (Api.false_data) {
			setFalseData();
			setFalseDataGrad();
		} else {

			loadingShoppingCart();
		}
		showPopWindow();
	}	
	
	@SuppressLint("InflateParams")
	private void showPopWindow() {
		//Button inOneWeek,inOneMonth,inThreeMonth,outThreeMonth;
		View myView;
    	
//    	ListView lv_comeraly_status_list;
		
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		myView = inflater.inflate(R.layout.commodity_select_size, null);

	
	    pw = new PopupWindow(myView, 0,0, true);
		pw.setFocusable(true);
		pw.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg_popupwindow));  
		pw.setOutsideTouchable(true);
		pw.setWidth(LayoutParams.MATCH_PARENT);  
		pw.setHeight(LayoutParams.WRAP_CONTENT);    
		pw.setAnimationStyle(R.style.AnimBottom);  
    }


	private void loadingShoppingCart() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 伪数据(轮播)
	 * */
		private void setFalseData() {
			ArrayList<BannerInfo> mInfos = new ArrayList<BannerInfo>();

			List<HashMap<String, String>> banner_list = new ArrayList<HashMap<String, String>>();

			HashMap<String, String> infomap = new HashMap<String, String>(); 

			//infomap.clear();
			infomap.put("mImgUrl",
					"http://pic31.nipic.com/20130718/12115012_113639688000_2.jpg");
			banner_list.add(infomap);

			// infomap.clear();
			HashMap<String, String> infomap1 = new HashMap<String, String>(); 
			infomap1.put("mImgUrl",
					"http://pic31.nipic.com/20130718/12115012_113639688000_2.jpg");
			banner_list.add(infomap1);

			// infomap.clear();
			HashMap<String, String> infomap2 = new HashMap<String, String>();
			infomap2.put("mImgUrl",
					"http://pic31.nipic.com/20130718/12115012_113639688000_2.jpg");
			banner_list.add(infomap2);

			for (int i = 0; i < banner_list.size(); i++) {
				BannerInfo info = new BannerInfo();

				info.mImgUrl = banner_list.get(i).get("mImgUrl");
				System.out.println("--------info.mImgUrl------" + info.mImgUrl);
				mInfos.add(info);
			}

			mPagerIndicator
					.initIndicator(0, mInfos.size(),
							R.drawable.indicator_dot_normal,
							R.drawable.indicator_dot_focus);

			BannerAdapter mBannerAdapter = new BannerAdapter(CommodityDetails.this, mInfos);
			mPagerAdapter = new LoopViewPagerAdapter(mBannerAdapter);
			mViewPager.setInfinateAdapter(mPagerAdapter);
			// 添加点击事件的监听

			mViewPager.setOnPageChangeListener(new BannerPageChangeListener(mInfos
					.size()));
			mViewPager.cancelScroll();
			if (mInfos.size() > 1) {
				mViewPager.startScroll();
			} else {
				mPagerIndicator.setVisibility(View.INVISIBLE);
			}

		}
		/**
		 * 伪数据(表格)
		 */
		private void setFalseDataGrad() {

			for (int i = 0; i <3 ; i++) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("imageUrl", "http://dh2.kimg.cn/static/images/public/20150812/bdd8b0d092ff2479d7453ad685c6005c.jpg");
				map.put("name", "衣服名字");

				map.put("categoryId",(i+1)+"");
				data_grid.add(map);
			}
			mAdapter_grid.update(data_grid);
		}
		/**
		 *
		 * */
		private void createAdapterGrid() {
			ViewCreator<HashMap<String, String>> creator = new ViewCreator<HashMap<String, String>>() {
				@SuppressLint("InflateParams")
				@Override
				public View createView(LayoutInflater inflater, int position,
						HashMap<String, String> map) {
					// TODO Auto-generated method stub
					
					return LayoutInflater.from(context).inflate(
							R.layout.item_grid_commodity, null);
				}   

				@Override
				public void updateView(View view, int position,
						final HashMap<String, String> map) {
					// TODO Auto-generated method stub
					
					ImageView img_parents = (ImageView) view.findViewById(R.id.img_parents);
					TextView tv_parents = (TextView) view.findViewById(R.id.tv_name);
					
					imageLoader.displayImage(map.get("imageUrl"), img_parents, Config.no_iv_option);//get图片
					tv_parents.setText(map.get("name"));//get文字

				}

			};

			 mAdapter_grid = new HolderAdapter<HashMap<String,String>>(this.getLayoutInflater(),creator);
			 gv.setAdapter(mAdapter_grid);
			 mAdapter_grid.update(data_grid);
			
		}
		private class BannerPageChangeListener implements OnPageChangeListener {
			//
			public static final int SCROLL_STATE_IDLE = 0;
			//
			public static final int SCROLL_STATE_DRAGGING = 1;
			//
			public static final int SCROLL_STATE_SETTLING = 2;

			private int mSize;

			public BannerPageChangeListener(int size) {
				this.mSize = size;
			}

			@Override
			public void onPageScrollStateChanged(int state) {
				switch (state) {
				case SCROLL_STATE_IDLE:
					break;

				case SCROLL_STATE_DRAGGING:
					break;

				case SCROLL_STATE_SETTLING:
					break;
				}
			}

			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
			}

			@Override
			public void onPageSelected(int position) {
				int index = position % mSize;
				mPagerIndicator.refreshIndicator(index);

			}

		}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 返回
		case R.id.iv_actionbar_side_left:
			finish();
			break;
			
		case R.id.select_size:
			if (pw.isShowing()) {
				pw.dismiss();// 关闭
			} else {
				pw.showAsDropDown(bg_actionbar_side);// 显示
				pw.update();
			}
			break;
		case R.id.immediately_buy:
			startActivity(new Intent(context,SelectAddress.class));
			break;
		default:
			break;
		}
	}
}

